//
//  SXZHNewsMainViewController.m
//  ShiXun
//
//  Created by lanou on 15/10/28.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXZHNewsMainViewController.h"
#import "SXZHNewsMainCell.h"
#import "SXZHNewsMainImageCell.h"
#import "SXRequestManager.h"
#import "SXDataBaseManager.h"
#import "SXZHNewsMainModel.h"
#import "SXZHDetailViewController.h"
@interface SXZHNewsMainViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation SXZHNewsMainViewController
- (UITableView *)mainTableView
{
    if (_mainTableView == nil) {
        _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 143)];
        _mainTableView.dataSource = self;
        _mainTableView.delegate = self;

        [_mainTableView registerClass:[SXZHNewsMainCell class] forCellReuseIdentifier:@"SXZHNewsMainCell"];
        [_mainTableView registerClass:[SXZHNewsMainImageCell class] forCellReuseIdentifier:@"SXZHNewsMainImageCell"];
    }
    return _mainTableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _start = 0;
    _limit = 1;
    [SXDataBaseManager DBManage:nil dataName:_URLString manageType:dataBaseManageTypeQueryData getDataFromDict:^(NSData *data) {
        if (data) {
            [self analyzeData:data];
        }
    }];
    [self requestData];
    // Do any additional setup after loading the view.
}
- (void)requestData
{
    NSString *URLStr = [kZHNewsMainListURLString stringByAppendingString:_URLString];
    if (_start == 0) {
        URLStr = URLStr;
    }else{
        URLStr = [URLStr stringByAppendingPathComponent:[NSString stringWithFormat:@"/before/%ld", ((SXZHNewsMainModel *)self.dataSourceArray.lastObject).Id]];
    }
    
    [SXRequestManager requestWithType:GET urlString:URLStr parDic:nil finish:^(NSData *data) {
        if (_start == 0) {
            [SXDataBaseManager DBManage:data dataName:_URLString manageType:dataBaseManageTypeStoreData getDataFromDict:nil];
            [self.dataSourceArray removeAllObjects];
        }
        [self analyzeData:data];
        [self removeLoadingView];
    } error:^(NSError *error) {
        NSLog(@"%@", error);
        [self removeLoadingView];
    }];
}
- (void)analyzeData:(NSData *)data
{
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    for (NSDictionary *ZHNewsDic in dic[@"stories"]) {
        SXZHNewsMainModel *model = [[SXZHNewsMainModel alloc] init];
        [model setValuesForKeysWithDictionary:ZHNewsDic];
        [self.dataSourceArray addObject:model];
    }
    [self.mainTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SXZHNewsMainModel *model = self.dataSourceArray[indexPath.row];
    if (model.images == nil) {
        SXZHNewsMainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SXZHNewsMainCell" forIndexPath:indexPath];
        cell.configureModel = model;
        return cell;
    }else
    {
        SXZHNewsMainImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SXZHNewsMainImageCell" forIndexPath:indexPath];
        cell.configureModel = model;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SXZHNewsMainModel *model = self.dataSourceArray[indexPath.row];
    if (model.images == nil) {
        return 75;
    }else{
        return 80;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SXZHNewsMainModel *model = self.dataSourceArray[indexPath.row];
    SXZHDetailViewController *ZHDetailVC = [[SXZHDetailViewController alloc] init];
    ZHDetailVC.IdStr = model.Id;
    [self.rootRightVC.navigationController pushViewController:ZHDetailVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
